/*******************************************************************************
 * @file    rtc_app.h
 * @author  Hickey.Liao
 * @date    2017-03-31
 * @brief   
 *
 * Copyright (C) 2017 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 *
 * @attention
 *
 ******************************************************************************/
#ifndef _RTC_APP_H
#define _RTC_APP_H
#include <stdbool.h>
#include <time.h>



/* GPS定位标志 */
#define RTC_GPS_NO_FIX      0X01
/* 每隔1S更新一次时间 */
#define RTC_UPDATE_TIM      1000
/* 每隔5分钟存储一次RTC时间 */
#define RTC_SAVE_TIM        300
/* 存储RTC标志 */
#define RTC_SAVE_FLAG       0X01


#define rtc_get_time(x)    bsp_rtc_get(x)
#define rtc_set_time(x)    bsp_rtc_set(x)


/* rtc运行信息 */
typedef struct {
    unsigned char gps_fix;
    unsigned int  rum_tim;
    unsigned int  save_tim;
    struct tm     rtc;
} rtc_info_t;


typedef struct
{
    unsigned short year;
    unsigned char month;
    unsigned char day;
    unsigned char hour;
    unsigned char min;
    unsigned char sec;
    unsigned char weekday; /* 0=Sunday */
} stdlib_time_struct;

typedef struct {
	unsigned char negative; 
    unsigned char hour; 
    unsigned char min;
    unsigned char dst;    
} stdlib_timezone_struct;


#include <stdbool.h>


#define HOURS_PER_SECOND    (3600ul)
#define MINUTE_PER_SECOND   (60ul)



#define TMA_MAX_UTC_TIME            14
#define TMA_MAX_NTP_DOMAIN_LEN      60

#define TMA_DEFAULT_NTP_SERVER      "ntp2.aliyun.com"
#define TMA_DEFAULT_NTP_PORT        123      


/*GTTMA配置参数 --------------------------------------------------------------*/
typedef struct {
    char     sign;
    unsigned char hour_offset;
    unsigned char minute_offset;
    unsigned char daylight_saving;
    char     utc_time[TMA_MAX_UTC_TIME + 1];
    char     ntp_server[TMA_MAX_NTP_DOMAIN_LEN + 1];  /*NTP Server IP / Domain Name*/
    unsigned short ntp_port;   //0~65535  /*NTP Server Port*/               
} tma_config_t;


extern void rtc_app_init(void); /* RTC初始化*/
extern void rtc_run_time(void); /* RTC运行计数*/
extern int rtc_get_time(struct tm *rtc);/* 获取RTC时间*/
extern int rtc_check_is_valid(struct tm time); /*检查时间合法*/
extern void debug_set_rtc_time(void);
extern int rtc_sync_time(const struct tm new_tm); /* 同步时间*/
extern int rtc_update_from_flash(void); /*设备上电更新RTC时间*/
extern void rtc_update_from_gps(void); /*gps同步rtc时间*/
extern void rtc_save_to_flash(unsigned char type);/* 存储RTC时间*/
extern void rtc_update_handler(void); /*更新时间*/
extern int applib_dt_get_rtc_time(stdlib_time_struct *time); /*返回RTC时间*/
extern int timetostr(stdlib_time_struct *t, char *buf, int len);
extern unsigned char get_time_sync_flg(void);
extern void tm_to_stdlib_time(struct tm *tm_time, stdlib_time_struct *std_time);
extern unsigned int get_rtc_sec(void);
extern  bool is_rtc_sec_timeout(unsigned int start_time, unsigned int timeout);
extern int applib_utc_get_rtc_time(stdlib_time_struct *time);
extern bool need_to_update_from_modem(void);
extern void rtc_update_from_modem(struct tm * new_time, int time_zone);

unsigned int get_timestamp(void); /*  获取utc时间戳(不带时区) */
unsigned int get_rtc_timestamp(void);  /* 获取RTC时间戳(本地时间戳使用，带时区)*/

#endif
